package com.skivingcloud.admin.sys.controller;


import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.fasterxml.jackson.databind.JsonNode;
import com.skivingcloud.admin.annotation.UserOperateLog;
import com.skivingcloud.admin.sys.entity.SysBusinessDesigner;
import com.skivingcloud.admin.sys.service.ISysBusinessDesignerService;
import com.skivingcloud.common.utils.PageUtils;
import com.skivingcloud.common.utils.R;


/**
 * <p>
 * 应用设计记录 前端控制器
 * </p>
 *
 * @author skivingcloud
 * @since 2022-07-04
 */
@RestController
@RequestMapping("/sys/businessDesigner")
public class SysBusinessDesignerController {
	private static Logger log = LoggerFactory.getLogger(SysBusinessDesignerController.class);

	@Autowired
	private ISysBusinessDesignerService sysBusinessDesignerService;

	/**
	 * 分页
	 */
	@PostMapping("/list")
	public R list(@RequestBody Map<String, Object> params) {
		/*Map<String, Object> params = new HashMap<>();
		params.put("page", page);
		params.put("limit", limit);
		params.put("dataForm", dataForm);*/
		PageUtils pages = sysBusinessDesignerService.queryPage(params);
		return R.ok().put("page", pages);
	}

	/**
	 * 新增
	 * 
	 * @param sysBusinessDesigner
	 * @return
	 */
	@UserOperateLog(module = "应用设计记录", methods = "save", description = "新增应用设计记录")
	@PostMapping("/save")
	public R save(@RequestBody SysBusinessDesigner sysBusinessDesigner) {
		try {
			sysBusinessDesignerService.add(sysBusinessDesigner);
			return R.ok();
		} catch (Exception e) {
			log.error("保存应用设计记录失败!", e);
			return R.error("保存应用设计记录失败!");
		}
	}

	/**
	 * 修改
	 * 
	 * @param sysBusinessDesigner
	 * @return
	 */
	@UserOperateLog(module = "应用设计记录", methods = "update", description = "修改应用设计记录")
	@PostMapping("/update")
	public R update(@RequestBody SysBusinessDesigner sysBusinessDesigner) {
		try {
			sysBusinessDesignerService.update(sysBusinessDesigner);
			return R.ok();
		} catch (Exception e) {
			log.error("保存应用设计记录失败!", e);
			return R.error("保存应用设计记录失败!");
		}
	}

	/**
	 * 删除
	 * 
	 * @param ids
	 * @return
	 */
	@UserOperateLog(module = "应用设计记录", methods = "delete", description = "删除应用设计记录")
	@PostMapping("/delete")
	public R delete(@RequestBody String[] ids) {
		try {
			sysBusinessDesignerService.deleteBatch(ids);
			return R.ok();
		} catch (Exception e) {
			log.error("删除应用设计记录失败!", e);
			return R.error("删除应用设计记录失败!");
		}
	}

	/**
	 * 根据主键查询
	 * 
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@GetMapping("/info/{id}")
	public R info(@PathVariable("id") String id) throws Exception {
		SysBusinessDesigner sysBusinessDesigner = sysBusinessDesignerService.selectById(id);
		return R.ok().put("data", sysBusinessDesigner);
	}
	
	/**
	 * 根据设计编码获取新增表单的设计json
	 * @param designerCode
	 * @return
	 * @throws Exception
	 */
	@GetMapping("/getFormJsonAddByBusDesignerCode/{designerCode}")
	public R getFormJsonAddByBusDesignerCode(@PathVariable("designerCode") String designerCode) throws Exception {
		JsonNode formJson = sysBusinessDesignerService.getFormJsonAddByBusDesignerCode(designerCode);
		return R.ok().put("formJson", formJson);
	}
	
	/**
	 * 根据设计编码获取包含表单json的设计
	 * 
	 * @param designerCode
	 * @return
	 * @throws Exception
	 */
	@GetMapping("/getSysBusinessDesignerByCodeHasJson/{designerCode}")
	public R getSysBusinessDesignerByCodeHasJson(@PathVariable("designerCode") String designerCode) throws Exception {
		SysBusinessDesigner sysBusinessDesigner = sysBusinessDesignerService.getSysBusinessDesignerByCodeHasJson(designerCode);
		return R.ok().put("data", sysBusinessDesigner);
	}
}
